package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.exception.SaveException;
import com.mrd.gtimp.business.entity.DeclarationPort;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2017/5/22.
 */
@Service
public class DeclarationPortBiz extends BaseBiz<DeclarationPort> {
    @Override
    @Resource
    @Qualifier("declarationPortDao")
    public void setBaseRepository(BaseRepository<DeclarationPort, String> baseRepository) {
        super.baseRepository = baseRepository;
    }

    /**
     * 保存的同时保存Company表
     * @param entity
     * @throws Exception
     */
    @Override
    public void saveOrupdateEntity(DeclarationPort entity) throws SaveException {
        super.saveOrupdateEntity(entity);
        try {
            String sql = "update t_sys_company set port='"+entity.getPortName()+"' where port_code='"+entity.getPortCode()+"'";
            this.updateBySql(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据申报口岸编码获取申报口岸
     * @param portCode
     * @return
     */
    public DeclarationPort findEntityByPortCode(String portCode){
        String hql = "from com.mrd.gtimp.business.entity.DeclarationPort where portCode='"+portCode+"'";
        List<DeclarationPort> list = this.findEntityByHql(hql,0,1,null);
        if(!list.isEmpty() && list.size()==1){
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据申报口岸查询是否需要SO号
     * @param portCode
     * @return
     */
    public boolean findHasSOByPortCode(String portCode){
        if(StringUtils.isNotEmpty(portCode)){
            DeclarationPort declarationPort = this.findEntityByPortCode(portCode);
            if(declarationPort==null){
                return false;
            }
            return declarationPort.getHasSO().equals("0");
        }
        return false;
    }

}
